package com.dems.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import com.dems.domain.Student;
import com.dems.util.JDBCUtils;

public class StudentDao {
	Connection conn = null;
	PreparedStatement ps = null;
	ResultSet rs = null;
	//根据学号查询学生
	public Student selectOne(String studentNum) {
		try {
            conn = JDBCUtils.getConnection();
            String sql = "select * from student where student_num=?";
            ps = conn.prepareStatement(sql);
            ps.setString(1,studentNum);
            rs = ps.executeQuery();
            if(rs.next()){
                Student student = new Student();
                student.setStudentId(rs.getInt("student_id"));
                student.setStudentNum(rs.getString("student_num"));
                student.setStudentName(rs.getString("student_name"));
                student.setStudentPassword(rs.getString("student_password"));
                student.setStudentDormitory(rs.getString("student_dormitory"));
                student.setBuildingId(rs.getString("building_id"));
                student.setStudentPhone(rs.getString("student_phone"));
                return student;
            }else{
               return null;
            }
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }finally {
            JDBCUtils.close(conn,ps,rs);
        }
	}
	
	//根据宿舍楼号查询学生
	public List<Student> selectStudentsByBuildingId(String buildingId) {
    	try {
            conn = JDBCUtils.getConnection();
            String sql = "select * from student where building_id=?";
            ps = conn.prepareStatement(sql);
            ps.setString(1,buildingId);
            rs = ps.executeQuery();
            List<Student> studentList = new ArrayList<>();
            while (rs.next()) {
            	Student student = new Student();
                student.setStudentId(rs.getInt("student_id"));
                student.setStudentNum(rs.getString("student_num"));
                student.setStudentName(rs.getString("student_name"));
                student.setStudentPassword(rs.getString("student_password"));
                student.setStudentDormitory(rs.getString("student_dormitory"));
                student.setBuildingId(rs.getString("building_id"));
                student.setStudentPhone(rs.getString("student_phone"));
                studentList.add(student);
            }
            return studentList;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }finally {
            JDBCUtils.close(conn,ps,rs);
        }
    }
	
	//学生注册
	public void registStudent(Student student) {
		try {
			conn = JDBCUtils.getConnection();
			ps = conn.prepareStatement("insert into student values(null,?,?,?,?,?,?)");
			ps.setString(1, student.getStudentNum());
			ps.setString(2, "");
			ps.setString(3, student.getStudentPassword());
			ps.setString(4, "");
			ps.setString(5,student.getBuildingId());
			ps.setString(6, "");
			ps.executeUpdate();
			
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}finally{
			JDBCUtils.close(conn, ps, null);
		}
	}
	
	//修改学生信息
	public boolean update(Student student) {
		int i = 0;
		try {
			conn = JDBCUtils.getConnection();
			ps = conn.prepareStatement("update student set student_name=?,student_password=?,student_dormitory=?,student_phone=? where student_num=?");
			ps.setString(1, student.getStudentName());
			ps.setString(2, student.getStudentPassword());
			ps.setString(3,student.getStudentDormitory());
			ps.setString(4, student.getStudentPhone());
			ps.setString(5, student.getStudentNum());
			i = ps.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
			return i>0;
		}finally{
			JDBCUtils.close(conn, ps, null);
		}
		return i>0;
	}
	
	//学生登录
	public Student login(String accountId, String password) {
		try {
            conn = JDBCUtils.getConnection();
            String sql = "select * from student where student_num=? and student_password=?";
            ps = conn.prepareStatement(sql);
            ps.setString(1,accountId);
            ps.setString(2,password);
            rs = ps.executeQuery();
            if(rs.next()){
                Student student = new Student();
                student.setStudentId(rs.getInt("student_id"));
                student.setStudentNum(rs.getString("student_num"));
                student.setStudentName(rs.getString("student_name"));
                student.setStudentPassword(rs.getString("student_password"));
                student.setStudentDormitory(rs.getString("student_dormitory"));
                student.setBuildingId(rs.getString("building_id"));
                student.setStudentPhone(rs.getString("student_phone"));
                return student;
            }else{
               return null;
            }
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }finally {
            JDBCUtils.close(conn,ps,rs);
        }
	}
	
	//忘记密码
	public boolean forget(String accountId, String phone, String password) {
		int i = 0;
		try {
            conn = JDBCUtils.getConnection();
            String sql = "update student set student_password=? where student_num=? and student_phone=?";
            ps = conn.prepareStatement(sql);
            ps.setString(1,password);
            ps.setString(2,accountId);
            ps.setString(3, phone);
            i = ps.executeUpdate(); 
        }catch (Exception e){
            e.printStackTrace();
            return i > 0;
        }finally {
            JDBCUtils.close(conn,ps,rs);
        }
		return i > 0;
	}

	
	
}
